//
//  PhysiologicalPeriodViewController.m
//  Lightthings
//
//  Created by 王岩 on 2024/11/29.
//

#import "PhysiologicalPeriodViewController.h"

@interface PhysiologicalPeriodViewController ()
@property (weak, nonatomic) IBOutlet UILabel *openLab;
@property (weak, nonatomic) IBOutlet UISwitch *switchBtn;
@property (weak, nonatomic) IBOutlet UILabel *startLab;
@property (weak, nonatomic) IBOutlet UILabel *durationLab;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet UIButton *durationBtn;

/* <#note#> */
@property (nonatomic, assign) long durationValue;
@property (nonatomic, assign) unsigned long switchValue, yearValue, mouthValue, dayValue;

@end

@implementation PhysiologicalPeriodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"生理期设置");
    
    self.openLab.text = SWString(@"On/Off");
    self.startLab.text = SWString(@"Timer");
    self.durationLab.text = SWString(@"持续时间");
    
    [self  updateUI];
    [self readData];
    
    [SWInitializer jy_initRedButtonWithTarget:self action:@selector(clickSureBtnAction) frame:CGRectMake(0, 0, kScreenWidth - 100, 50) title:SWString(@"Confirm") on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(50);
        kMakeBV(-100);
        kMakeWHV(kScreenWidth-100, 50);
    }];
    
    // Do any additional setup after loading the view from its nib.
}
- (void)updateUI {
    if (self.switchBtn.on) {
        self.startBtn.hidden = self.durationBtn.hidden = self.startLab.hidden = self.durationLab.hidden = NO;
    
        NSString * start = [NSString stringWithFormat:@"20%02lu-%02lu-%02lu",self.yearValue, self.mouthValue,self.dayValue];
        NSString * duration = [NSString stringWithFormat:@"%lu%@",self.durationValue, SWString(@"天")];
        
        [self.startBtn setTitle:start forState:0];
        
        [self.durationBtn setTitle:duration forState:0];
        
    }else {
        self.startBtn.hidden = self.durationBtn.hidden = self.startLab.hidden = self.durationLab.hidden = YES;
    }
}

- (void)readData {
    [[WatchCommandTool sharedInstance] readStormCommand:Command_PhysiologicalPeriod ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
        if (backData) {
            NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
            unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
            self.switchValue = value;
            NSLog(@"开关数值：%lu",value);
            // 开关状态（0：关闭，1：打开）
            if (value == 1) {
                self.switchBtn.on = YES;
                valueStr = [backData substringWithRange:NSMakeRange(28, 2)];
                value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
                NSLog(@"开始时间_年：%lu",value);
                self.yearValue = value;
                
                valueStr = [backData substringWithRange:NSMakeRange(30, 2)];
                value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
                NSLog(@"开始时间—月：%lu",value);
                self.mouthValue = value;
                
                valueStr = [backData substringWithRange:NSMakeRange(32, 2)];
                value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
                NSLog(@"开始时间—天：%lu",value);
                self.dayValue = value;
                
                valueStr = [backData substringWithRange:NSMakeRange(34, 2)];
                value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
                NSLog(@"间隔时间：%lu",value);
                self.durationValue = value;
                
            }else {
                self.switchBtn.on = NO;
            }
            
            [self updateUI];
            
        }
    }];
}

- (void)clickSureBtnAction {
    
}


- (IBAction)clickSwitchBtn:(UISwitch *)sender {
    [self updateUI];
}

- (IBAction)clickStartBtn:(UIButton *)sender {
    [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:nil selectValue:nil resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        [self.startBtn setTitle:selectValue forState:0];
    }];
}
- (IBAction)clickDurationBtn:(UIButton *)sender {
    __block UITextField *tf = nil;
    [LEEAlert alert].config
    .LeeTitle(SWString(@"持续时间"))
    .LeeContent(SWString(@"Time-lapse minute must be within 1-255 minutes"))
    .LeeAddTextField(^(UITextField *textField) {
        // 这里可以进行自定义的设置
        textField.placeholder = SWString(@"Enter time-lapse (minute)");
        if (@available(iOS 13.0, *)) {
            textField.textColor = [UIColor secondaryLabelColor];
        } else {
            textField.textColor = [UIColor darkGrayColor];
        }
        textField.keyboardType = UIKeyboardTypeNumberPad;
        tf = textField; //赋值
    })
    .LeeAction(SWString(@"Confirm"), ^{
        self.durationValue = tf.text.longLongValue;
        [self updateUI];
    })
    .leeShouldActionClickClose(^(NSInteger index){
        // 是否可以关闭回调, 当即将关闭时会被调用 根据返回值决定是否执行关闭处理
        // 这里演示了与输入框非空校验结合的例子
        BOOL result = ![tf.text isEqualToString:@""];
        result = index == 0 ? result : YES;
        return result;
    })
    .LeeCancelAction(SWString(@"Cancel"), nil) // 点击事件的Block如果不需要可以传nil
    .LeeShow();
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
